package com.mycjj.android.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.StoreAdapter;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.DistanceAscComparator;
import com.mycjj.android.bean.DistanceDescComparator;
import com.mycjj.android.bean.StoreBaseBean;
import com.mycjj.android.bean.StoreBaseBean.Store;
import com.mycjj.android.bean.TreeElement;
import com.mycjj.android.bean.YuyueMonthStateBean;
import com.mycjj.android.bean.YuyueMonthStateBean.YuyueData;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.util.Utils;

/**
 * 绑定服务点界面
 * 
 * @author mark
 * 
 */
public class SelectStoreActivity extends BaseActivity {

	private String token;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_store);
		mActManager.pushActivity(YUYUE_KEY, this);
		initHeader();
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("绑定服务点");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mActManager.popActivity(YUYUE_KEY, SelectStoreActivity.this);
			}
		});
	}


	private ListView mListview;
	private TAdapter<Store> adapter;

	private String type;
	private int action;
	private String carId;
	private Store currentStore;
	private View rl_item_01,rl_item_02,rl_item_03;
	private TextView tv_city;
	private void init() {
		token = Preferences.getSharedPreferences(this, "user_token", "");
		Intent intent = getIntent();
		if(intent != null){
			type = intent.getStringExtra("type");
			action = intent.getIntExtra("action", 0);
			carId = intent.getStringExtra("carId");
		}
		
		Log.e("mark", "token:"+token+";type:"+type+";carId:"+carId);
		if(TextUtils.isEmpty(token)){
			Toast.makeText(this, "会话超时!", Toast.LENGTH_LONG).show();
			return;
		}
		Log.d("mark", "SelectStoreActivity token:"+token+";type:"+type+";action:"+action);
		
		rl_item_01 = findViewById(R.id.rl_item_01);
		rl_item_02 = findViewById(R.id.rl_item_02);
		rl_item_03 = findViewById(R.id.rl_item_03);
		tv_city = (TextView)findViewById(R.id.tv_temp3);
		if(!TextUtils.isEmpty(mApp.getNowCity())){
			tv_city.setText(mApp.getNowCity());
		}
		rl_item_01.setOnClickListener(viewOnClickListener);
		rl_item_02.setOnClickListener(viewOnClickListener);
		mListview = (ListView) findViewById(R.id.listview);
		adapter = new StoreAdapter(this);
		mListview.setAdapter(adapter);
		mListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				if (storeList != null && storeList.size() > 0) {
					if (position >= storeList.size()) {
						Log.e("mark", "position:" + position
								+ ";storeList.size:" + storeList.size()
								);
						return;
					}
					
					Log.i("info", "--ethan--"+currentStore.getStore_points());
					currentStore = storeList.get(position);
					dataManager.setCurrentStore(currentStore);
					dataManager.setStoreChange(true);
					Preferences.setEditor(mApp, "storeId", String.valueOf(currentStore.getStore_id()));
					Preferences.setEditor(mApp, "storeName", currentStore.getStore_name());
					Preferences.setEditor(mApp, "storeAddress", currentStore.getStore_address());
					Preferences.setEditor(mApp, "storeTel", currentStore.getStore_tel());
					Preferences.setEditor(mApp, "storePoint", currentStore.getStore_points());
					if(action == 0){
						mActManager.popActivity(YUYUE_KEY, SelectStoreActivity.this);
					}else{
						getPlanState();//继续向下走
					}
				}
			}
		});
	}
	
	private OnClickListener viewOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.rl_item_01:
				initData();
				break;
			case R.id.rl_item_02:
				if(isDesc){//当前降序
					isDesc = false;
					//升序
					Collections.sort(storeList, new DistanceAscComparator());
				}else{//当前升序
					isDesc = true;
					//降序
					Collections.sort(storeList, new DistanceDescComparator());
				}
				adapter.setList(storeList);
				break;
			}
		}
	};
	
	private boolean isDesc = false;

	private List<Store> storeList;

	private void initData() {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		// TODO:获取定位城市名称
		params.put("area_id", String.valueOf(mApp.getNowCityId()));
		params.put("limit", "10");
		params.put("offset", "0");
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", token);
		TreeElement treeElement = dataManager.getElement();
		if(treeElement != null){
			params.put("brand_id", treeElement.getBrandId());
		}else{
			params.put("brand_id", "0");
		}
		params.put("points", mApp.getPoints());
		
		request.requestStoreGetStoresWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						StoreBaseBean baseBean = ProjectDataManage.getInstance().getStoreBaseBean();
						if (null != baseBean) {
							if (baseBean.getData() != null) {
								storeList = baseBean.getData();
								if (storeList != null && storeList.size() > 0) {
									adapter.setList(storeList);
									currentStore = storeList.get(0);
								}
							}

						}

					}
				});
	}
	
	private void getPlanState(){
		String  storeId = Preferences.getSharedPreferences(mApp,"storeId", "");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		String month = cal.get(Calendar.YEAR)+"-"+Utils.getMonth(cal.get(Calendar.MONTH) + 1);
		Log.d("mark", "storeId:"+storeId+";month:"+month);
		RequestParams params = new RequestParams();
		params.put("store_id", storeId);
		params.put("month", month);
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request = new ProjectHttpRequestImpl(this, true);
		request.requestStoreGetMonthPlanWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				Log.i("info", "----ethan==="+content);
				YuyueMonthStateBean bean = dataManager.getYuyueMonthStateBean();
				if(bean.getCode() == 200){
					Intent intent = new Intent(SelectStoreActivity.this,SelectYuyueDateActivity.class);
					intent.putExtra("type", type);
					intent.putExtra("dataList", (ArrayList<YuyueData>)bean.getData());
					intent.putExtra("carId", carId);
					startActivity(intent);
				}else{
					Toast.makeText(SelectStoreActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}
